extends Node2D

export (int) var index
var CanOpen = false
# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	$Label.visible = false


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass
func _input(event): 
	if event.is_action_pressed("Z") and CanOpen:
		CommonSignals.emit_signal("GamePause")
		$Sprite.texture = load("res://art/Door/DoorOpen.png")
		yield(get_tree().create_timer(1),"timeout")
#		GlobalVar.TrueDoor = 2
		if index == GlobalVar.TrueDoor:
			CommonSignals.emit_signal("GamePause")
			var doorend = load(GlobalVar.TrueEndPath).instance()
			get_parent().get_parent().add_child(doorend)
#			yield(get_tree().create_timer(12),"timeout")
#			get_parent().get_parent().free()
#			doorend.free()
#			CommonSignals.emit_signal("EnemyCaughtPlayer")
		else:
			var doorindex = randi()%GlobalVar.DoorPath.size()
#			doorindex=3
			var doorend = load(GlobalVar.DoorPath[doorindex]).instance()
			get_parent().get_parent().add_child(doorend)
			if doorindex == 3:
				yield(get_tree().create_timer(63),"timeout")
			yield(get_tree().create_timer(1),"timeout")
			doorend.free()
		CommonSignals.emit_signal("GamePause")	
			

func _on_Door_body_entered(body):
	pass
		
		


func _on_Door_body_exited(body):
	pass


func _on_Area2D_body_entered(body):
	if body.name == "Player":
		$Label.visible = true
		CommonSignals.emit_signal("PlayerNearDoor")
		CanOpen = true


func _on_Area2D_body_exited(body):
	if body.name == "Player":
		$Label.visible = false
		$Sprite.texture = load("res://art/Door/DoorCLosed.png")
		CommonSignals.emit_signal("PlayerLeaveDoor")
		CanOpen = false
